package com.yehongyu.mytools.poidoc;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.sl.usermodel.Notes;
import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.xslf.XSLFSlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.xmlbeans.XmlException;

public class PptUtil {
	
	private static final Logger logger = Logger.getLogger(PptUtil.class);

	public static String readPowerPoint(String path) {
		StringBuffer content = new StringBuffer("");
		try {
			SlideShow ss = new SlideShow(new HSLFSlideShow(new FileInputStream(path)));// is
			// 为文件的InputStream，建立SlideShow
			Slide[] slides = ss.getSlides();// 获得每一张幻灯片
			for (int i = 0; i < slides.length; i++) {
				TextRun[] t = slides[i].getTextRuns();// 为了取得幻灯片的文字内容，建立TextRun
				for (int j = 0; j < t.length; j++) {
					content.append(t[j].getText());// 这里会将文字内容加到content中去
				}
			}
		} catch (IOException ex) {
			logger.error("exception io error!",ex);
		}
		return content.toString();
	}
	
	public static String readPowerPoint2007(String path) {
		StringBuffer content = new StringBuffer("");
		try {
			XMLSlideShow ss = new XMLSlideShow(new XSLFSlideShow(path));
			// 为文件的InputStream，建立SlideShow
			XSLFSlide[] slides = ss.getSlides();// 获得每一张幻灯片
			for (int i = 0; i < slides.length; i++) {
				Notes n = slides[i].getNotes();// 建立Notes
//				slides[i].getShapes();
				if(n!=null){
					org.apache.poi.sl.usermodel.TextRun t = n.getTextRun();
					content.append(t.getText());// 这里会将文字内容加到content中去
					Shape[] shapes = n.getShapes();
					for(int m = 0;m<shapes.length;m++){
//						shapes[m].get
					}
				}
			}
		} catch (IOException ex) {
			logger.error("exception io error!",ex);
		} catch (XmlException e) {
			logger.error("XmlException error!",e);
		} catch (OpenXML4JException e) {
			logger.error("OpenXML4JException error!",e);
		}
		return content.toString();
	}
	
	
	
	public static void main(String[] args){
//		System.out.println(PptUtil.readPowerPoint("\\\\YingYang\\software\\Office2003.ppt"));
		System.out.println(PptUtil.readPowerPoint2007("\\\\YingYang\\software\\北京Qcon之行.pptx"));
	}

}
